import threading
import queue
from collections import deque
from sqlhand import DbHandle
from toolhand import *


class MultiGetUrl(threading.Thread):
    def __init__(self, task_queue, thread_name, logger, lock):
        threading.Thread.__init__(self, name=thread_name)
        self.queue = task_queue
        self.lock = lock
        self.logger = logger
        self.tmp = []

    def run(self):
        while True:
            info = self.queue.get()
            if self.lock.locked():
                self.tmp.append(info)
                pass
            else:
                self.lock.acquire()
                db = DbHandle(self.logger)
                dbfile = "todayb.db"
                db.db_connect(dbfile)
                db.init_database()
                print(info, self.getName())
                for tt in self.tmp:
                    '''allstar(tt, db)'''
                self.tmp = []
                self.lock.release()
            self.queue.task_done()


def main():
    start = dict()
    start['url'] = "https://www.nsfw.xxx"
    logop = dict()
    logop['logFile'] = "log.txt"
    logop['logLevel'] = "INFO"
    global global_logger
    global_logger = getlog(logop)
    global global_db
    global_db = DbHandle(global_logger)
    dbfile = "todayb.db"
    global_db.db_connect(dbfile)
    global_db.init_database()
    '''global_db.select_urls()'''
    global_db.insert_one(start, 'urls')
    global_queue = queue()
    thread_num = 3
    lock = threading.Lock()
    for i in range(thread_num):
        print("thread:", i)
        t1 = MultiGetUrl(queue, 'urlt_' + str(i), global_logger, lock)
        t1.setDaemon(True)
        t1.start()
    allurl = global_db.select_urls()
    
    while True:
        print("in main while")
        while len(allurl) > 0:
            t = allurl.pop()
            print(t)
            global_queue.put(t)
        allurl = global_db.select_urls()
        global_queue.join()


if __name__ == "__main__":
    print("hello world\n")
    main()